package com.eco.fanliapp.controller.app;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eco.fanliapp.common.base.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.AppUser;
import com.eco.fanliapp.entity.SyTimePeriod;
import com.eco.fanliapp.entity.SyTimePeriodAppointment;
import com.eco.fanliapp.entity.SyTimePeriodGoods;
import com.eco.fanliapp.service.ISyTimePeriodAppointmentService;
import com.eco.fanliapp.service.ISyTimePeriodGoodsService;
import com.eco.fanliapp.service.ISyTimePeriodService;
import com.eco.fanliapp.service.ITaoBaoService;
import com.eco.fanliapp.utils.DateFormatUtils;
import com.eco.fanliapp.utils.DateUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-09-09
 */
@RestController
@RequestMapping("/app/syTimePeriodGoods")
public class SyTimePeriodGoodsController extends BaseController {



    @Autowired
    private ISyTimePeriodService syTimePeriodService;

    @Autowired
    private ISyTimePeriodGoodsService iSyTimePeriodGoodsService;

    @Autowired
    private ISyTimePeriodAppointmentService iSyTimePeriodAppointmentService;

    @Autowired
    private ITaoBaoService iTaoBaoService;

    @GetMapping(value = "list")
    public ServerResponse list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, Integer tableId,@RequestParam(defaultValue = "0")Integer hotCake) {
        if(tableId == null){
            return  ServerResponse.createByErrorMessage("参数有误");
        }
        SyTimePeriod syTimePeriod = syTimePeriodService.getById(tableId);
        if(StringUtils.isEmpty(syTimePeriod))return  ServerResponse.createByErrorMessage("参数有误");
        QueryWrapper queryWrapper = new QueryWrapper();
        String[] split = syTimePeriod.getPeriodTime().split(":");
        int hours = new Date().getHours();
        if(hours < Integer.valueOf(split[0]) && hotCake == 0){
        }else if(hours < Integer.valueOf(split[0]) && hotCake == 1){
            queryWrapper.eq("hot_cake", -1);
        }else{
            queryWrapper.eq("hot_cake", hotCake);
        }
        String strDate = DateUtils.date2Str(new Date(), DateUtils.DATE_FORMAT_YYYYMMDD);
        queryWrapper.eq("enable", 1);
        queryWrapper.eq("deleted", 0);
        queryWrapper.eq("period_id", tableId);
        queryWrapper.orderByDesc("hot_cake");
        queryWrapper.orderByAsc("top_serial_num");
        queryWrapper.between("period_goods_time", strDate + " 00:00:00", strDate + " 23:59:59");
        Page<SyTimePeriodGoods> page = new Page<>(pageNum, pageSize);
        IPage iPage = iSyTimePeriodGoodsService.page(page, queryWrapper);
        List<SyTimePeriodGoods> syTimePeriodGoodsList = iPage.getRecords();
        //查询用户预约的商品
        //判断商品抢购状态
        /*if(CollectionUtils.isNotEmpty(syTimePeriodGoodsList) && hotCake == 1){
            SyTimePeriodGoods syTimePeriodGoods = syTimePeriodGoodsList.get(0);
            String periodTime = syTimePeriodGoods.getPeriodTime();
            String[] split = periodTime.split(":");
            int hours = new Date().getHours();
            if(hours < Integer.valueOf(split[0])){
                syTimePeriodGoodsList.removeAll(syTimePeriodGoodsList);
            }
        }*/
        AppUser appUser = getAppUser();
        if (!StringUtils.isEmpty(appUser)) {
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("user_id", appUser.getUserId());
            queryWrapper1.between("item_price_date", strDate + " 00:00:00", strDate + " 23:59:59");
            List<SyTimePeriodAppointment> syTimePeriodAppointmentList = iSyTimePeriodAppointmentService.list(queryWrapper1);
            if (CollectionUtils.isNotEmpty(syTimePeriodAppointmentList) && CollectionUtils.isNotEmpty(syTimePeriodGoodsList)) {
                for (SyTimePeriodGoods syTimePeriodGoods : syTimePeriodGoodsList) {
                    for (SyTimePeriodAppointment syTimePeriodAppointment : syTimePeriodAppointmentList) {
                            if(syTimePeriodGoods.getItemId().longValue() == syTimePeriodAppointment.getItemId().longValue()){
                                syTimePeriodGoods.setAppointment(1);
                                break;
                            }
                    }
                }
            }
        }
        Long userId = null;
        if(appUser != null){
            userId = appUser.getUserId();
        }
        List<SyTimePeriodGoods> syTimePeriodAppointmentListNew = iTaoBaoService.calculatedGoodsCommission(userId,syTimePeriodGoodsList);

        iPage.setRecords(syTimePeriodAppointmentListNew);
        return ServerResponse.createBySuccess("查询成功", iPage);
    }
}
